layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //渲染时间选择框
    laydate.render({
        elem: "#month",
        value: month,
        type: 'month',
        tiger: 'click'
    });

    /**
     * 导入Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/btyRbsc/import'
        , accept: "file"
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (res) {
            if (res.state == 0) {
                layer.msg('导入失败！', {icon: 1, time: 2000, shade: 0.2});
            }
            if (res == true) {
                layer.msg('导入成功！', {icon: 1, time: 2000, shade: 0.2});
                window.location.reload();
            }
            layer.close(loadIndex);
        }
    });
});

//当前月份
let monthNum = new Date().getMonth() + 1;
const month = new Date().getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);

//初始化页面
$(function () {
    getHeight()
    date();
    let data = {
        date: month + "-01",
        last: month + "-33",
    };
    list(data);
});
function getHeight(){
    let ava = document.documentElement.clientHeight
    let height = ava-55
    document.getElementById("table").style.height= height + "px"; 
};
// 重置按钮事件
$('#btnReset').click(function () {
    $('input').val('');
});
// 搜索按钮点击事件
$('#btnSearch').on('click', function () {
    let str = $("#latest").val().substring(0, 10);
    let strings = $("#month").val().split("-");
    let st = str.split("-");
    if (strings[1] != st[1]) {
        str = $("#month").val() + "-33";
    } else {
        if(str.substring(9) != 0){
            str = str.substring(0, 8) + (Number(str.substring(str.length-2,str.length)) + Number(1));
        } else {
            str = str.substring(0, 8) + (Number(str.charAt(str.length - 1)) + Number(1));
        }
    }
    //查询数据
    let data = {
        date: $("#month").val() + "-01",
        last: str,
        reporter: $("#reporter").val(),
        storeCode: $("#storeCode").val(),
        storeName: $("#storeName").val()
    };
    list(data, 32);
});

/**
 * 导出excel按钮
 */
$('#exportExcel').on('click', function () {
    $("#tbody").find("tr").each(function () {
        $(this).children().eq(0).remove();
    });
    var html = "<html><head><meta charset='utf-8' /></head><body>" + $("#table").html() + "</body></html>";
    var blob = new Blob([html], {
        type: "application/vnd.ms-excel",
        name: "name"
    });
    var a = document.getElementById("exportExcel");
    a.download = $("#month").val().replace('-', '年') + "月日报审查列表.xlsx";
    a.href = URL.createObjectURL(blob);
});

//获取最新日期
function date() {
    $.ajax({
        type: "post",
        url: "/btyRbsc/selectDate",
        datatype: "json",
        async: false,
        success: function (data) {
            $("#latest").val(data);
            let substr = data.substring(data.length - 13);
            let strings = substr.split(" ");
            let day = strings[0];
            let time = strings[1];
            if (time < 12) {
                day -= 1;
            }
            $("#day").val(day);
            for (let i = 1; i <= day; i++) {
                $("#tr").append("<th class='de'>" + i + "</th>");
            }
        }
    });
}

//查询数据
function list(data, a) {
    $("#tbody").empty();
    let days = "";
    let num = $("#day").val();
    if (a == 32) {
        num = a-1;
        $('.de').remove();
    }
    for (let q = 0; q < num ; q++) {
        if (a == 32) {
            $("#tr").append("<th class='de'>" + (Number(q) + Number(1)) + "</th>");
        }
        days += "<td>×</td>";
    }
    $.ajax({
        type: "post",
        url: "/btyRbsc/list",
        datatype: "json",
        data: data,
        async: false,
        success: function (result) {
            if(result!=null){
                for (let i = 0; i < result.length; i++) {
                    let number = Number(i) + Number(1);
                    $("#tbody").append('<tr class="' + i + '">' +
                        '<td hidden>' + result[i].dates + '</td>\n' +
                        '<td>' + number + '</td>\n' +
                        '<td>' + result[i].reporter + '</td>\n' +
                        '<td>' + result[i].storeCode + '</td>\n' +
                        '<td>' + result[i].storeName + '</td>\n' + days +
                        '</tr>');
                }
                let status = "×";
                $("#noticeTable").find("tr:not(:first)").each(function () {
                    let split = $(this).children().eq(0).text().split(",");
                    for (let j = 0; j < split.length; j++) {
                        let split1 = split[j].split("--");
                        let comments = split1[0];
                        let date = j + 5;
                        if (split1[1] != undefined) {
                            if (split1[1].substring(0, 7) == data.date.substring(0, 7)) {
                                date = Number(split1[1].substring(split1[1].length - 2)) + Number(4);
                                if (comments > 0) {
                                    status = "√";
                                } else {
                                    status = "Ο";
                                }
                            } else {
                                status = "x";
                            }
                        } else {
                            status = "x";
                        }
                        $(this).children().eq(date).html(status);
                    }
                });
            }
        }
    });
}